package com.asman.worker.multishow

import android.annotation.SuppressLint
import android.app.Activity
import android.app.ActivityOptions
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.asman.customerview.MyOnPageChangeListener
import com.asman.worker.R
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import kotlinx.android.synthetic.main.activity_multi_show.*
import kotlin.math.min

class MultiShowActivity : AppCompatActivity() {

    companion object {
        const val IMAGES = "images"
        const val CURRENT_INDEX = "current_index"
        fun intentFor(context: Context, images: List<ImageTitle?>, current: Int): Intent {
            val intent = Intent(context, MultiShowActivity::class.java)
            intent.putExtra(IMAGES, Gson().toJson(images))
            intent.putExtra(CURRENT_INDEX, current)
            return intent
        }

        fun start(context: Activity, images: List<ImageTitle?>, current: Int) {
            val intent = intentFor(context, images, current)
            context.startActivity(
                intent, ActivityOptions.makeSceneTransitionAnimation(context).toBundle()
            )
        }

        fun intentFor(context: Context, json: String?, current: Int): Intent {
            val intent = Intent(context, MultiShowActivity::class.java)
            intent.putExtra(IMAGES, json)
            intent.putExtra(CURRENT_INDEX, current)
            return intent
        }

        fun start(context: Activity, json: String, current: Int) {
            val intent = intentFor(context, json, current)
            context.startActivity(
                intent, ActivityOptions.makeSceneTransitionAnimation(context).toBundle()
            )
        }

        //todo 图片和视频展示测试数据
        fun testStart(context: Activity) {
            val images = arrayListOf<ImageTitle>()
            images.add(
                ImageTitle(
                    "https://asman-app.oss-cn-hangzhou.aliyuncs.com/video/app_test1.mp4",
                    "aaaa"
                )
            )
            images.add(
                ImageTitle(
                    "https://asman-app.oss-cn-hangzhou.aliyuncs.com/video/1557729048909811.mp4",
                    "bbbbbbb"
                )
            )
            images.add(
                ImageTitle(
                    "https://asman-img.oss-cn-hangzhou.aliyuncs.com/06_6d0961efa4032f6214e13b10ca8744f7.png",
                    "ccccc"
                )
            )
            images.add(
                ImageTitle(
                    "https://asman-app.oss-cn-hangzhou.aliyuncs.com/video/1557729048909811.mp4",
                    "dddddddd"
                )
            )
            images.add(
                ImageTitle(
                    "https://asman-app.oss-cn-hangzhou.aliyuncs.com/video/app_test.mp4",
                    "eeeeeeeeee"
                )
            )
            images.add(
                ImageTitle(
                    "https://asman-img.oss-cn-hangzhou.aliyuncs.com/06_6d0961efa4032f6214e13b10ca8744f7.png",
                    "fffffff"
                )
            )
            images.add(
                ImageTitle(
                    "https://asman-app.oss-cn-hangzhou.aliyuncs.com/video/app_test1.mp4",
                    "ggggg"
                )
            )
            images.add(
                ImageTitle(
                    "https://asman-img.oss-cn-hangzhou.aliyuncs.com/064f607c8b3ec89ec15f5cb2b61923a7_c7f409723f7655153ec02c23b87b31a9.jpg",
                    "hhhhhhhh"
                )
            )
            start(context, images, 2)
        }
    }

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_multi_show)
//        StatusBarUtil.setTranslucentForImageView(this, 0, null)
//        StatusBarUtil.setDarkModel(this)
        val images: List<ImageTitle> = Gson().fromJson(
            intent.getStringExtra(IMAGES), object : TypeToken<List<ImageTitle>>() {}.type
        )
        val current = intent.getIntExtra(CURRENT_INDEX, 0)
        val adapter = object :
            MultiShowAdapter<ImageTitle>(images, this, false, object : CallBack<ImageTitle> {
                override fun getUrl(t: ImageTitle) = t.url ?: ""
            }) {}
        view_pager.adapter = adapter
        view_pager.offscreenPageLimit = 0
        view_pager.currentItem = current
        view_pager.addOnPageChangeListener(object : MyOnPageChangeListener() {
            override fun onPageSelected(position: Int) {
                tv_index.text = "${position + 1}/"
                tv_title.text = images[position].title
                adapter.stopOtherVideo()
            }
        })
        tv_index.text = "${min(current + 1, images.size)}/"
        tv_count.text = "${images.size}"
        tv_title.text = images[current].title
        iv_back.setOnClickListener {
            onBackPressed()
        }
    }

    override fun onBackPressed() {
        //        super.onBackPressed()
        finish()
    }
}
